package cn.com.duiba.millionaire.center.biz.service.impl;

import cn.com.duiba.millionaire.center.api.dto.MillionaireActivityDto;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireGameDao;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireQuestionBagDao;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireGameEntity;
import cn.com.duiba.millionaire.center.biz.service.MillionaireActivityService;
import cn.com.duiba.millionaire.center.biz.service.MillionaireGameService;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by sunchangji on 2018/3/7.
 */
@Service
public class MillionaireGameServiceImpl implements MillionaireGameService {
    private static final Logger LOGGER = LoggerFactory.getLogger(MillionaireGameServiceImpl.class);
    @Autowired
    private MillionaireGameDao millionaireGameDao;
    @Autowired
    private MillionaireQuestionBagDao millionaireQuestionBagDao;
    @Autowired
    private MillionaireActivityService millionaireActivityService;

    /**
     * 游戏是否最后一题cache
     */
    private final Cache<String, Boolean> gameLastIndexCache = CacheBuilder.newBuilder().expireAfterWrite(20, TimeUnit.MINUTES).maximumSize(10000).build();

    @Override
    public boolean gameInsert(MillionaireGameEntity millionaireGameEntity) {
        MillionaireGameEntity entity = millionaireGameDao.gameInsert(millionaireGameEntity);
        boolean result = entity.getId() > 0;
        if (result) {
            millionaireQuestionBagDao.changeQuestionNum(entity.getBagId(), 1);
        }
        return result;
    }

    @Override
    public boolean updateById(MillionaireGameEntity millionaireGameEntity) {
        return millionaireGameDao.updateById(millionaireGameEntity) == 1;
    }

    @Override
    public MillionaireGameEntity findById(Long id) {
        return millionaireGameDao.findById(id);
    }

    @Override
    public List<MillionaireGameEntity> findByBagId(Long bagId) {
        return millionaireGameDao.findByBagId(bagId);
    }

    @Override
    public boolean deleteById(Long id) {
        MillionaireGameEntity entity = millionaireGameDao.findById(id);
        if (entity == null) {
            return false;
        }
        boolean result = millionaireGameDao.deleteById(id) == 1;
        if (result) {
            millionaireQuestionBagDao.changeQuestionNum(entity.getBagId(), -1);
        }
        return result;
    }

    @Override
    public Boolean isLastOne(Long activityId, Integer index) {
        String key = activityId + "_" + index;
        Boolean isLastIndex = gameLastIndexCache.getIfPresent(key);
        if (isLastIndex != null) {
            return isLastIndex;
        }
        MillionaireActivityDto activityDto = millionaireActivityService.findMillionaireActivityById(activityId);
        if (activityDto == null) {
            LOGGER.warn("没有查到题目信息, activityId={}, gameIndex={}", activityId, index);
            throw new IllegalArgumentException("没有查到活动信息");
        }
        Long total = activityDto.getQuestionNum();
        isLastIndex = index == total.intValue();
        gameLastIndexCache.put(key, isLastIndex);
        return isLastIndex;
    }

}
